package main

import (
	"fmt"
	"io"
	"openstack_api/orchestration"
	"os"
	//"github.com/Ivy1996-encode/ginrest"
	"github.com/gin-gonic/gin"
	log "github.com/sirupsen/logrus"
	"openstack_api/orchestration/orchestrationstack"
)

func main() {
	//Default返回一个默认的路由引擎
	_, e := os.Stat("/var/log/")
	if e == nil {
		f, _ := os.OpenFile("/var/log/openstack_api_access.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0766)
		gin.DefaultWriter = io.MultiWriter(f)
	} else {
		f, _ := os.OpenFile("openstack_api_access.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0766)
		gin.DefaultWriter = io.MultiWriter(f)
	}

	SetLogger()
	log.Info("Openstack api service start")

	r := gin.Default()
	gin.ForceConsoleColor()
	r.GET("/ping", func(c *gin.Context) {
		//输出json结果给调用方
		c.JSON(200, gin.H{
			"message": "pong",
		})
	})

	stack := orchestrationstack.OrchestrationStack{}
	//r.GetStack("stack", new(orchestrationstack.OrchestrationStack).GetStack)
	//r.CreateStack("stack", new(orchestrationstack.OrchestrationStack).CreateStack)
	fmt.Println(&stack)
	r.GET("stack/:name", stack.GetStack)
	r.GET("stack", stack.GetStack)
	r.GET("stack/:name/callback", stack.GetCallbackData)
	r.POST("stack", stack.CreateStack)
	r.PUT("stack/:name", stack.UpdateStack)
	r.GET("stack/:name/resources", stack.ListResources)
	r.GET("stack/:name/resources/:resource", stack.ListResources)
	r.DELETE("stack/:name", stack.DeleteStack)

	image := orchestration.Image{}
	r.POST("/images", image.CreateImage)
	r.GET("/images", image.ListImages)
	r.GET("/images/:uuid", image.GetImage)
	r.PUT("/images/:uuid", image.UpdateImage)
	r.DELETE("/images/:uuid", image.DeleteImage)

	flavor := orchestration.Flavor{}
	r.POST("/flavors", flavor.CreateFlavor)
	r.GET("/flavors", flavor.GetFlavors)
	r.DELETE("/flavors/:flavor_id", flavor.DelFlavors)

	server := orchestration.Server{}
	r.POST("/servers/:uuid/console", server.GetConsole)
	r.GET("/servers", server.ListServers)
	r.GET("/servers/:uuid", server.GetServer)
	r.POST("/servers/:uuid/stop", server.StopServer)
	r.POST("/servers/:uuid/start", server.StartServer)
	r.POST("/servers/:uuid/reboot", server.RebootServer)
	r.POST("/servers/:uuid/os-instance_file", server.PostInstanceFile)
	r.GET("/servers/:uuid/os-instance_file", server.GetInstanceFile)
	r.POST("/servers/:uuid/vnc-screenshot", server.CreateVncScreenShot)
	r.DELETE("/servers/:uuid/vnc-screenshot/:filename", server.DeleteVncScreenShot)
	r.GET("/servers/:uuid/vnc-screenshot/:filename", server.GetVncScreenShot)
	r.POST("/servers/:uuid/vnc-screencap", server.CreateVncScreenCap)
	r.PUT("/servers/:uuid/vnc-screencap", server.StopVncScreenCap)
	r.DELETE("/servers/:uuid/vnc-screencap/:filename", server.DeleteVncScreenCap)
	r.GET("/servers/:uuid/vnc-screencap/:filename", server.GetVncScreenCap)
	//r.POST("/servers/:uuid/metrics", server.Metrics)

	r.GET("/services/compute", orchestration.GetHyperService)
	r.PUT("/services/compute/:host_id", orchestration.UpdateHyperService)

	volume := orchestration.Volume{}
	r.POST("/volumes/:uuid/upload-to-image", volume.UploadToImage)
	r.POST("/volumes", volume.CreateVolume)
	r.DELETE("/volumes/:uuid", volume.DeleteVolume)

	err := r.Run(":8006")
	if err != nil {
		return
	} // listen and serve on 0.0.0.0:8080
}

func SetLogger() {
	//if os.Getuid() != 0{
	//	fmt.Println("Need root to run")
	//	os.Exit(1)
	//}

	log.SetFormatter(&log.JSONFormatter{
		TimestampFormat: "2006-01-02 15:04:05",
		FieldMap: log.FieldMap{
			log.FieldKeyTime:  "@timestamp",
			log.FieldKeyLevel: "@level",
			log.FieldKeyMsg:   "@message",
			log.FieldKeyFunc:  "@caller",
		},
	})
	_, e := os.Stat("/var/log/")
	logFile := ""
	if e == nil {
		logFile = "/var/log/openstack_api.log"
	} else {
		logFile = "openstack_api.log"
	}
	log.SetReportCaller(true)
	file, err := os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
	mw := io.MultiWriter(os.Stdout, file)
	if err != nil {
		panic(fmt.Sprintf("Log file %s not found", mw))
	}
	log.SetOutput(mw)
}
